<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Brand;
use Auth;
use DB;
use App\Carmodel;

class BrandsController extends Controller
{
  
     public function cmlist(){
          
          $cars = Brand::get();
          $model = new Brand();
          $types = Brand::get();
         
          // $data = $this->getTree($carss,$b_parent = 1,$lev=1);
          //  // var_dump($data);exit;
        $data = Brand::where('b_parent', '-1')->paginate(14);
        return view('admin.carmodel_list',['data'=>$data,'cars'=>$cars,'types'=>$types]);
    }

       public function addcm(Request $req){
        if (empty($_POST)) {  
        $cars = DB::table('brands')->where('b_parent', '-1')->get();
        return view('admin.add_carmodel',['cars'=>$cars]);
        }else{
          dd($req->all());
        }
    }

       public function edit($b_id){
         if(empty($_POST)){
            $brand = new Brand;  
            $brand = Brand :: find($b_id);
            // dd($brand);
            return view('admin.edit_carstyle',['brand'=>$brand]);
         } 

       }

       public function editModel($b_id){
         if(empty($_POST)){
            $brand = new Brand;  
            $brand = Brand :: find($b_id);    
            $cars = DB::table('brands')->where('b_parent', '-1')->get();
            return view('admin.edit_carmodel',['cars'=>$cars]);
         } 

       }

     public function del($b_id){  
            $brand = new Brand;  
            $brand = Brand :: find($b_id);
            return $brand->delete()? 'OK':'fail';
    }

    // public function pp(){
    //      $cars = DB::table('brands')->where('b_parent', '-1')->get();
    //      return view('admin/carmodel_list',['cars'=>$cars]);
    // }

    //联动菜单
    public function getcx(){
        $bid = $_GET['b_id'];
        $carss = DB::table('brands')->where('b_id',$bid)->first();
        $b_id = $carss->b_id;
        $cha = DB::table('brands')->where('b_parent',$b_id)->get();
        echo json_encode($cha);
    }


       
     public function getTree($cars,$b_parent = 1,$lev=1){  
        // var_dump($cars);exit;
        $tree = array();
       
        //print_r ($cars);
        foreach($cars as $k=>$v){
            if($v['b_parent'] == $b_parent){
                $v['lev'] = $lev;
                $tree[] = $v;
                $tree = array_merge($tree,$this->getTree($cars,$v['b_id'],$lev+1));
            }
        }
        return $tree;
      }

      public function ishot(){
        $hot = DB::table('cars')->where('ishot',1)->take(4)->get();
        // print_r($hot);
        echo json_encode($hot);
      }
     //更多热门
       public function ishots(){
        $hots = DB::table('cars')->where('ishot',1)->get();
        echo json_encode($hots);
      }


      public function isshow(){
        $show = DB::table('cars')->where('isshow',1)->take(4)->get();
        echo json_encode($show);
      }

    //更多推荐
      public function isshows(){
        $shows = DB::table('cars')->where('isshow',1)->get();
        echo json_encode($shows);
      }

       public function isrecom(){
        $recom = DB::table('cars')->where('isrecom',1)->take(4)->get();
        // print_r($recom);exit;
        echo json_encode($recom);
      }
    //更多最新
        public function isrecoms(){
        $recoms = DB::table('cars')->where('isrecom',1)->get();
        // print_r($recoms);exit;
        echo json_encode($recoms);
      }

      public function hy(){
        return view('home.hot');
      }
}
